﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace WEB_Browser
{
    public partial class Form1 : Form
    {
        //该区域内是自定义的函数和全局变量
        #region 
        
        private int urlNum=0;

        //浏览器控件中打开链接
        private void OpenURLInBrowser(string url)
        {
            if (!url.StartsWith("http://") && !url.StartsWith("https://"))
            {
                url = "http://" + url;
            }
            try
            {
                this.webBrowser1.Navigate(new Uri(url));
                this.toolStripStatusLabel1.Text = url;
            }
            catch (System.UriFormatException)
            {
                this.toolStripStatusLabel1.Text = "网页链接打开失败！";
                return;
            }
        }

        //获取listview中的内容
        private string getListviewContent()
        {
            string listViewContent = "";
            try
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    foreach(ListViewItem.ListViewSubItem sub in item.SubItems)
                    {
                        listViewContent += sub.Text.Trim() + "\t";
                    }
                    listViewContent = listViewContent.TrimEnd() + "\r\n";
                }
            }
            catch
            {
                this.toolStripStatusLabel1.Text = "获取全部抓取内容时失败";
            }
            return listViewContent;
        }

        //获取listview中指定列的内容
        private string getListviewListContent(int listNum)
        {
            string listViewListContent = "";
            try
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    listViewListContent += item.SubItems[listNum].Text.Trim() + "\r\n";
                }
            }
            catch
            {
                this.toolStripStatusLabel1.Text = "获取全部抓取URL时失败";
            }
            return listViewListContent;
        }

        //获取listview中URL的主机部分
        private string getListviewUrlSite()
        {
            string listViewUrlSite = "";
            try
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    Uri itemUrl = new Uri(item.SubItems[1].Text.Trim());
                    listViewUrlSite += "http://" + itemUrl.Host + "\r\n";
                }
            }
            catch
            {
                this.toolStripStatusLabel1.Text = "获取全部抓取Site时失败";
            }
            return listViewUrlSite;
        }

        #endregion

        public Form1()
        {
            InitializeComponent();
            //
        }

        //选择下拉框后的操作
        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectSearch = this.toolStripComboBox1.Text;
            switch (selectSearch)
            {
                case "google":
                    this.toolStripTextBox1.Text = "http://www.google.com.hk";
                    this.toolStripTextBox1.Focus();
                    break;
                case "bing":
                    this.toolStripTextBox1.Text = "http://www.bing.com";
                    this.toolStripTextBox1.Focus();
                    break;
                case "baidu":
                    this.toolStripTextBox1.Text = "http://www.baidu.com";
                    this.toolStripTextBox1.Focus();
                    break;
                case "soso":
                    this.toolStripTextBox1.Text = "http://www.soso.com";
                    this.toolStripTextBox1.Focus();
                    break;
                case "yahoo.cn":
                    this.toolStripTextBox1.Text = "http://www.yahoo.cn/";
                    this.toolStripTextBox1.Focus();
                    break;
                case "yahoo.com":
                    this.toolStripTextBox1.Text = "http://search.yahoo.com/";
                    this.toolStripTextBox1.Focus();
                    break;
                case "youdao":
                    this.toolStripTextBox1.Text = "http://www.youdao.com/";
                    this.toolStripTextBox1.Focus();
                    break;
                case "other":
                    this.toolStripTextBox1.Focus();
                    break;
                default:
                    break;
            }
        }

        //打开网页按钮
        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.toolStripTextBox1.Text) || this.toolStripTextBox1.Text.Equals("about:blank"))
            {
                MessageBox.Show("Enter a valid URL.");
                this.toolStripTextBox1.Focus();
                return;
            }
            OpenURLInBrowser(this.toolStripTextBox1.Text);
        }

        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Refresh();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoBack();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoForward();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoHome();
        }

        //点击网页中的链接，不在启动IE，而是使用webbrowser继续浏览
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            if (webBrowser1.Document.ActiveElement != null)
            {
                webBrowser1.Navigate(webBrowser1.Document.ActiveElement.GetAttribute("href"));
                this.toolStripTextBox1.Text = webBrowser1.Document.ActiveElement.GetAttribute("href");
            }
        }

        //每次浏览网页后，combox_url中的文本变成当前网页的url
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            string currentURL = webBrowser1.Url.ToString();
            this.toolStripTextBox1.Text = currentURL.ToString();
            if (currentURL.Length < 40)
            {
                this.toolStripStatusLabel1.Text = currentURL;
            }
            else
            {
                this.toolStripStatusLabel1.Text = currentURL.Substring(0, 40) + "...";
            }
        }

        //显示网页另存为对话框
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowSaveAsDialog();
            //webBrowser1.Document.ExecCommand("saveas", true, "C:\\");
        }

        //显示打印对话框
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintDialog();
        }

        //显示打印预览对话框
        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintPreviewDialog();
        }

        //显示网页另存为对话框
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.DocumentText = "";
        }

        //显示网页另存为对话框
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //显示打开网页文件对话框
        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "网页文件(*.html;*.htm;*.mht)|*.html;*.htm;*.mht|所有文件(*.*)|*.*";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fileName = this.openFileDialog1.FileName;
                    this.webBrowser1.Navigate(fileName);
                }
                catch (Exception)
                {
                    MessageBox.Show("Error", "Can't open the file");
                }
            }
        }

        //main 抓取按钮的操作
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            try
            {
                //注释的部分是为了检测页面是否加载完毕，没有加载完毕的情况会让选择是否继续
                //if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
                //{
                //    DialogResult dr = MessageBox.Show("是否继续抓取", "页面正忙",MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                //    if(dr == DialogResult.No)
                //    {
                //        return;
                //    }
                //}

                //以下一句是为了方式页面内容为空的时候程序报错
                Uri currentUrl_temp = this.webBrowser1.Url;
                string temp = currentUrl_temp.Host;
            }
            catch
            {
                toolStripStatusLabel1.Text = "抓取失败！";
                return;
            }
            
            //Uri currentUrl = this.webBrowser1.Url;
            //string curUrlHost = currentUrl.Host;
            if (this.toolStripTextBox1.Text.Contains("google")) //对对google的搜索结果进行处理
            {
                if (webBrowser1.Document.Body.InnerHtml.Contains("<h3 class=\"r\">"))
                {
                    //抓取搜索链接
                    foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("h3"))
                    {
                        listView1.Update();
                        listView1.Items.Add((urlNum+1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].GetAttribute("href"));
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].InnerText);
                        listView1.EndUpdate();
                        urlNum++;
                    }
                }
                else if (webBrowser1.Document.Body.InnerHtml.Contains("&amp;imgurl=http://"))
                {
                    //抓取图片链接
                    Regex regImge = new Regex("&imgurl=(.*?)&w=");
                    Regex regSite = new Regex("&imgrefurl=(.*?)&");

                    foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("a"))
                    {
                        string imgHref = oneItem.GetAttribute("href");
                        if (imgHref.Contains("http://www.google.com.hk/imgres?q="))
                        {
                            Match matchImge = regImge.Match(imgHref);
                            Match matchSite = regSite.Match(imgHref);
                            if (matchImge.Success && matchSite.Success)
                            {
                                listView1.Update();
                                listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                                listView1.Items[urlNum].SubItems.Add(matchImge.Groups[1].ToString());
                                listView1.Items[urlNum].SubItems.Add(matchSite.Groups[1].ToString());
                                listView1.EndUpdate();
                                urlNum++;
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("支持的抓取形式暂不支持", "Sorry");
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
            else if (this.toolStripTextBox1.Text.Contains("bing"))
            {
                //if (webBrowser1.Document.Body.InnerHtml.Contains("<div class=\"sw_aat\">网页</div>") || webBrowser1.Document.Body.InnerHtml.Contains("<div class=sw_aat>网页</div>"))
                if (!webBrowser1.DocumentTitle.Contains("- 必应 Bing 图片"))
                {
                    //抓取bing的网页搜索页面
                    try
                    {
                        foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("a"))
                        {
                            if (oneItem.Parent.OuterHtml.Contains("h3"))
                            {
                                listView1.Update();
                                listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                                listView1.Items[urlNum].SubItems.Add(oneItem.GetAttribute("href").ToString().Trim());
                                listView1.Items[urlNum].SubItems.Add(oneItem.InnerText.ToString());
                                listView1.EndUpdate();
                                urlNum++;
                            }
                        }
                    }
                    catch
                    {
                        toolStripStatusLabel1.Text = "页面抓取失败！";
                    }
                }
                //else if (webBrowser1.Document.Body.InnerHtml.ToLower().Contains("<div class=\"sw_aat\">图片</div>") || webBrowser1.Document.Body.InnerHtml.ToLower().Contains("<div class=sw_aat>图片</div>"))
                else if (webBrowser1.DocumentTitle.Contains("- 必应 Bing 图片"))
                {
                    //抓取bing的图片搜索页面  
                    //抓取图片链接
                    Regex regImge = new Regex("\",imgurl:\"http://(.*?)\",offset:");

                    Match matchImge = regImge.Match(webBrowser1.Document.Body.InnerHtml);
                    while (matchImge.Success)
                    {                        
                        Uri tempUrl = new Uri("http://" + matchImge.Groups[1].ToString());
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(tempUrl.ToString());
                        listView1.Items[urlNum].SubItems.Add(tempUrl.Host);
                        listView1.EndUpdate();
                        urlNum++;
                        matchImge = matchImge.NextMatch();
                    }
                }
                else
                {
                    MessageBox.Show("支持的抓取形式暂不支持", "Sorry");
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
            else if (this.toolStripTextBox1.Text.Contains("baidu"))
            {
                if (webBrowser1.DocumentTitle.Contains("百度搜索_"))
                {
                    //抓取搜索链接
                    foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("h3"))
                    {
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].GetAttribute("href"));
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].InnerText);
                        listView1.EndUpdate();
                        urlNum++;
                    }
                }
                else if (webBrowser1.DocumentTitle.Contains("百度图片搜索_"))
                {
                    //抓取图片链接
                    Regex regImge = new Regex("onclick=\"p\\(event,1,{sr:'0',u:'(.*?)',f:this.href,t:1}\\);");
                    Match matchImge = regImge.Match(webBrowser1.Document.Body.InnerHtml);
                    while (matchImge.Success)
                    {
                        Uri tempUrl = new Uri(matchImge.Groups[1].ToString());
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(tempUrl.ToString());
                        listView1.Items[urlNum].SubItems.Add(tempUrl.Host);
                        listView1.EndUpdate();
                        urlNum++;
                        matchImge = matchImge.NextMatch();
                    }
                }
                else
                {
                    MessageBox.Show("支持的抓取形式暂不支持", "Sorry");
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
            else if (this.toolStripTextBox1.Text.Contains("yahoo.cn"))
            {
                if (webBrowser1.DocumentTitle.Contains("_网页搜索"))
                {
                    //抓取搜索链接
                    foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("h3"))
                    {
                        if (oneItem.Children.Count == 0)
                        {
                            continue;
                        }
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].GetAttribute("href"));
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].InnerText);
                        listView1.EndUpdate();
                        urlNum++;
                    }
                }
                else if (webBrowser1.DocumentTitle.Contains("_雅虎图片搜索"))
                {
                    //抓取图片链接
                    Regex regImge = new Regex(",'img_src':'(.*?)','");

                    Match matchImge = regImge.Match(webBrowser1.Document.Body.InnerHtml);
                    while (matchImge.Success)
                    {
                        Uri tempUrl = new Uri(matchImge.Groups[1].ToString());
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(tempUrl.ToString());
                        listView1.Items[urlNum].SubItems.Add(tempUrl.Host);
                        listView1.EndUpdate();
                        urlNum++;
                        matchImge = matchImge.NextMatch();
                    }
                }
                else
                {
                    MessageBox.Show("支持的抓取形式暂不支持", "Sorry");
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
            else if (this.toolStripTextBox1.Text.Contains("yahoo.com"))
            {
                if (webBrowser1.DocumentTitle.Contains("- Yahoo! Search Results"))
                {
                    //抓取搜索链接
                    foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("h3"))
                    {
                        if (oneItem.GetElementsByTagName("a").Count == 0)
                        {
                            continue;
                        }
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].GetAttribute("href"));
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].InnerText);
                        listView1.EndUpdate();
                        urlNum++;
                    }
                }
                else if (webBrowser1.DocumentTitle.Contains("Image Search Results"))
                {
                    //抓取图片链接
                    Regex regImge = new Regex("&amp;imgurl=(.*?)&amp;rurl=");

                    Match matchImge = regImge.Match(webBrowser1.Document.Body.InnerHtml);
                    while (matchImge.Success)
                    {
                        Uri tempUrl = new Uri("http://" + matchImge.Groups[1].ToString().Replace("%2F", "/"));
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(tempUrl.ToString());
                        listView1.Items[urlNum].SubItems.Add(tempUrl.Host);
                        listView1.EndUpdate();
                        urlNum++;
                        matchImge = matchImge.NextMatch();
                    }
                }
                else
                {
                    MessageBox.Show("支持的抓取形式暂不支持", "Sorry");
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
            else if (this.toolStripTextBox1.Text.Contains("youdao"))
            {
                if (webBrowser1.DocumentTitle.Contains("- 有道搜索"))
                {
                    //抓取搜索链接
                    foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("h3"))
                    {
                        if (oneItem.GetElementsByTagName("a").Count == 0)
                        {
                            continue;
                        }
                        listView1.Update();
                        listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].GetAttribute("href"));
                        listView1.Items[urlNum].SubItems.Add(oneItem.GetElementsByTagName("a")[0].InnerText);
                        listView1.EndUpdate();
                        urlNum++;
                    }
                }
                else if (webBrowser1.DocumentTitle.Contains("- 有道图片搜索"))
                {
                    //抓取图片链接
                    Regex regImge = new Regex("','http%3A%2F%2F(.*?)', '");

                    foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("a"))
                    {
                        string imgHref = oneItem.OuterHtml;
                        if (imgHref.Contains("imagesa?q="))
                        {
                            Match matchImge = regImge.Match(imgHref);
                            if (matchImge.Success)
                            {
                                Uri picUrl = new Uri("http://" + matchImge.Groups[1].ToString().Replace("%2F", "/"));
                                listView1.Update();
                                listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                                listView1.Items[urlNum].SubItems.Add(picUrl.ToString());
                                listView1.Items[urlNum].SubItems.Add(picUrl.Host);
                                listView1.EndUpdate();
                                urlNum++;
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("支持的抓取形式暂不支持", "Sorry");
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
            else if (this.toolStripTextBox1.Text.Contains("soso"))
            {
                if (webBrowser1.DocumentTitle.Contains(" - 搜搜更懂你"))
                {
                    try
                    {
                        foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("h3"))
                        {
                            if (oneItem.FirstChild.GetAttribute("href") != "")
                            {
                                listView1.Update();
                                listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                                listView1.Items[urlNum].SubItems.Add(oneItem.FirstChild.GetAttribute("href").ToString().Trim());
                                listView1.Items[urlNum].SubItems.Add(oneItem.FirstChild.InnerText.ToString());
                                listView1.EndUpdate();
                                urlNum++;
                            }
                        }
                    }
                    catch
                    {
                        toolStripStatusLabel1.Text = "页面抓取失败！";
                    }
                }
                else
                {
                    MessageBox.Show("支持的抓取形式暂不支持", "Sorry");
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
            else
            {
                foreach (HtmlElement oneItem in webBrowser1.Document.GetElementsByTagName("a"))
                {
                    if (oneItem.GetAttribute("href").Contains("http://") || oneItem.GetAttribute("href").Contains("http%3A%2F%2F"))
                    {
                        try
                        {
                            Uri picUrl = new Uri(oneItem.GetAttribute("href").ToString().ToLower().Replace("%2f", "/").Replace("%2a", ":"));
                            listView1.Update();
                            listView1.Items.Add((urlNum + 1).ToString(), urlNum);
                            listView1.Items[urlNum].SubItems.Add(picUrl.ToString());
                            listView1.Items[urlNum].SubItems.Add(picUrl.Host);
                            listView1.EndUpdate();
                            urlNum++;
                        }
                        catch
                        {
                            toolStripStatusLabel1.Text = "页面抓取失败！";
                        }
                    }
                }
                toolStripStatusLabel1.Text = "已经抓取链接数： " + urlNum.ToString();
            }
        }

        //定义的时钟控件，定时刷新窗口右下角的时间
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel2.Text = DateTime.Now.ToString("MM-dd hh:mm:ss");
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        //网页加载进度条
        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            toolStripProgressBar1.Visible = true;

            if ((e.CurrentProgress > 0) && (e.MaximumProgress > 0))
            {

                toolStripProgressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);//设置正在加载的文档总字节数

                toolStripProgressBar1.Step = Convert.ToInt32(e.CurrentProgress);////获取已下载文档的字节数

                toolStripProgressBar1.PerformStep();

            }

            else if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)//加载完成后隐藏进度条
            {
                toolStripProgressBar1.Value = 0;
                toolStripProgressBar1.Visible = false;
            }
        }

        //清空listview中的内容
        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            urlNum = 0;
            toolStripStatusLabel1.Text = "抓取的链接数据已清空";
        }

        //判断主URL输入框的按键是否是enter，再决定是否打开
        private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue != 13)
            {
                return;
            }
            toolStripButton8_Click(sender, e);
        }

        //左键点击listview的item，实现浏览器打开链接（含右键选择打开功能）
        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            //将ListView的FullRowSelect属性设置为True；这样单击某一行的时候，就会选中单击的一整行了。
            try
            {
                int selectItemCount = listView1.SelectedIndices.Count;
                if (selectItemCount > 0)
                {
                    //选中某行，浏览器打开,只能打开单行，效果不好
                    //int lines = Convert.ToInt32(listView1.SelectedItems[0].Text);
                    //string oneUrl = listView1.Items[lines-1].SubItems[1].Text.ToString().Trim();
                    //System.Diagnostics.Process.Start(oneUrl);

                    for (int i = 0; i < selectItemCount; i++)
                    {
                        System.Diagnostics.Process.Start(listView1.SelectedItems[i].SubItems[1].Text.ToString().Trim());
                    }
                    //System.Diagnostics.Process.Start("explorer.exe",oneUrl);
                }
            }
            catch
            {
                toolStripStatusLabel1.Text = "链接打开失败！";
            }
        }

        //删除listview选中的行
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            try
            {
                int selectItemCount = listView1.SelectedIndices.Count;
                if (selectItemCount > 0)
                {
                    for (int i = 0; i < selectItemCount; i++)
                    {
                        listView1.SelectedItems[0].Remove();
                    }                    
                }
                urlNum = urlNum - selectItemCount;
                toolStripStatusLabel1.Text = "链接删除成功！";
            }
            catch
            {
                toolStripStatusLabel1.Text = "链接删除失败！";
            }
        }

        //copy listview中选中的行
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            try
            {
                int selectItemCount = listView1.SelectedIndices.Count;
                if (selectItemCount > 0)
                {
                    string strToCopy = "";
                    for (int i = 0; i < selectItemCount; i++)
                    {
                        strToCopy += listView1.SelectedItems[i].SubItems[0].Text.ToString().Trim() + "\t" +
                                     listView1.SelectedItems[i].SubItems[1].Text.ToString().Trim() + "\t" +
                                     listView1.SelectedItems[i].SubItems[2].Text.ToString().Trim() + "\r\n";
                    }
                    Clipboard.SetText(strToCopy.TrimEnd());
                }
                toolStripStatusLabel1.Text = "内容复制成功！";
            }
            catch
            {
                toolStripStatusLabel1.Text = "内容复制失败！";
            }
        }

        //copy listview中选中的url
        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            try
            {
                int selectItemCount = listView1.SelectedIndices.Count;
                if (selectItemCount > 0)
                {
                    string strToCopy = "";
                    for (int i = 0; i < selectItemCount; i++)
                    {
                        strToCopy += listView1.SelectedItems[i].SubItems[1].Text.ToString().Trim() + "\r\n";
                    }
                    Clipboard.SetText(strToCopy.TrimEnd());
                }
                toolStripStatusLabel1.Text = "URL复制成功！";
            }
            catch
            {
                toolStripStatusLabel1.Text = "URL复制失败！";
            }
        }

        //从listview中导出所有抓取到的结果
        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            try
            {
                string strToWrite = getListviewContent();
                //实例化一个保存文件对话框
                SaveFileDialog sf = new SaveFileDialog();
                //设置保存文件类型
                sf.Filter = "txt文件|*.txt|所有文件|*.*";
                //如果用户没有输入扩展名，自动追加后缀
                sf.AddExtension = true;
                
                sf.Title = "导出全部抓取内容";
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    //实例化一个文件流--->与写入文件相关联
                    FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                    //实例化一个StreamWriter-->与fs相关联
                    StreamWriter sw = new StreamWriter(fs);
                    //开始写入
                    sw.Write(strToWrite);
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                    toolStripStatusLabel1.Text = "导出文件成功！";
                }
            }
            catch
            {
                toolStripStatusLabel1.Text = "导出文件失败！";
            }

        }

        //从listview中导出所有links
        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            try
            {
                string strToWrite = getListviewListContent(1);
                //实例化一个保存文件对话框
                SaveFileDialog sf = new SaveFileDialog();
                //设置保存文件类型
                sf.Filter = "txt文件|*.txt|所有文件|*.*";
                //如果用户没有输入扩展名，自动追加后缀
                sf.AddExtension = true;

                sf.Title = "导出抓取的URL";
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    //实例化一个文件流--->与写入文件相关联
                    FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                    //实例化一个StreamWriter-->与fs相关联
                    StreamWriter sw = new StreamWriter(fs);
                    //开始写入
                    sw.Write(strToWrite);
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                    toolStripStatusLabel1.Text = "导出抓取的URL成功！";
                }
            }
            catch
            {
                toolStripStatusLabel1.Text = "导出抓取的URL失败！";
            }
        }

        //从listview中导出所有site（link的主域名部分）
        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            try
            {
                string strToWrite = getListviewUrlSite();
                //实例化一个保存文件对话框
                SaveFileDialog sf = new SaveFileDialog();
                //设置保存文件类型
                sf.Filter = "txt文件|*.txt|所有文件|*.*";
                //如果用户没有输入扩展名，自动追加后缀
                sf.AddExtension = true;

                sf.Title = "导出抓取的Site";
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    //实例化一个文件流--->与写入文件相关联
                    FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                    //实例化一个StreamWriter-->与fs相关联
                    StreamWriter sw = new StreamWriter(fs);
                    //开始写入
                    sw.Write(strToWrite);
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                    toolStripStatusLabel1.Text = "导出抓取的Site成功！";
                }
            }
            catch
            {
                toolStripStatusLabel1.Text = "导出抓取的Site失败！";
            }
        }

        //About对话框
        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form2 formAdout = new Form2();
            formAdout.ShowDialog();
        }

        //Home Page 打开网页
        private void homePageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", "http://vipscu.blog.163.com/");
        }

        

    }
}
